package client;


import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import client.MainJFrame;
/**
 *GUI class that show the client connection  
 * @author Group 19
 */
public class StartCon extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	/**
     * Creates new form StartCon
     */
    int port;
    Client tc;
    String host;
    
    public StartCon() {        
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        hostLabel = new javax.swing.JLabel();
        portLabel = new javax.swing.JLabel();
        hostTextField = new javax.swing.JTextField();
        portTextField = new javax.swing.JTextField();
        startButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        errorLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(600, 325));
        setResizable(false);
        getContentPane().setLayout(null);

        hostLabel.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
        hostLabel.setForeground(new java.awt.Color(0, 153, 153));
        hostLabel.setText("Host");
        getContentPane().add(hostLabel);
        hostLabel.setBounds(390, 40, 60, 30);

        portLabel.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
        portLabel.setForeground(new java.awt.Color(0, 153, 153));
        portLabel.setText("Port");
        getContentPane().add(portLabel);
        portLabel.setBounds(390, 80, 60, 30);

        hostTextField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        hostTextField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        hostTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                hostTextFieldKeyPressed(evt);
            }
        });
        getContentPane().add(hostTextField);
        hostTextField.setBounds(450, 50, 140, 19);

        portTextField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        portTextField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        portTextField.setMaximumSize(new java.awt.Dimension(20, 120));
        portTextField.setMinimumSize(new java.awt.Dimension(20, 120));
        portTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                portTextFieldKeyPressed(evt);
            }
        });
        getContentPane().add(portTextField);
        portTextField.setBounds(450, 90, 140, 19);

        startButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        startButton.setText("Connect");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });
        getContentPane().add(startButton);
        startButton.setBounds(490, 150, 100, 25);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/obl/images/client connection.jpg"))); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(0, 0, 600, 300);

        errorLabel.setBackground(new java.awt.Color(255, 100, 100));
        errorLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        errorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        errorLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        errorLabel.setMaximumSize(new java.awt.Dimension(600, 34));
        errorLabel.setMinimumSize(new java.awt.Dimension(600, 34));
        errorLabel.setOpaque(true);
        errorLabel.setPreferredSize(new java.awt.Dimension(600, 34));
        getContentPane().add(errorLabel);
        errorLabel.setBounds(0, 266, 600, 34);

        pack();
    }// </editor-fold>

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        this.startButton.setEnabled(false);
        this.host=this.hostTextField.getText();
        String temp;
        temp=this.portTextField.getText();
        
        if(this.host.isEmpty() || temp.isEmpty())
        {
           this.errorLabel.setVisible(true);
           this.errorLabel.setText("Warning: input is inncorrect"); 
           this.startButton.setEnabled(true);
           this.portTextField.setText(null);
           this.hostTextField.setText(null);
        }
        else{  
            try{
            	Integer.valueOf(temp);  
            	this.port=Integer.parseInt(temp);
            	try{
            		tc= new Client(this.host,this.port);
            		this.setVisible(false);
            		java.awt.EventQueue.invokeLater(new Runnable() {
            			public void run() {
            				new MainJFrame(tc).setVisible(true);
            			}
            		});
            		System.out.println("GUI created");
            		
            	} catch (Exception ea) {
            		// TODO Auto-generated catch block
            		this.startButton.setEnabled(true);
            		this.portTextField.setText(null);
            		this.hostTextField.setText(null);
            		this.errorLabel.setText("failed to open connection at " +	"host=" + this.host + "  " + "port=" + this.port);
            	}
            }catch (NumberFormatException en){
            	this.startButton.setEnabled(true);
        		this.portTextField.setText(null);
        		this.hostTextField.setText(null);
        		this.errorLabel.setText("Warning: port input must be numbers!");            	
            }
        }        
    }                                           

    private void hostTextFieldKeyPressed(java.awt.event.KeyEvent evt) {
        if(evt.getKeyCode()==KeyEvent.VK_ENTER)
            startButtonActionPerformed(new ActionEvent(this, 0, " "));
    }

    private void portTextFieldKeyPressed(java.awt.event.KeyEvent evt) {
      if(evt.getKeyCode()==KeyEvent.VK_ENTER)
            startButtonActionPerformed(new ActionEvent(this, 0, " "));
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StartCon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StartCon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StartCon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StartCon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StartCon().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel hostLabel;
    private javax.swing.JTextField hostTextField;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel portLabel;
    private javax.swing.JTextField portTextField;
    private javax.swing.JButton startButton;
    // End of variables declaration
}
